﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class StateDAO
    {
        DataServices m_StateDAO = new DataServices();

        public DataTable GetAll()
        {
            m_StateDAO.Load("GetAllState", null);
            return m_StateDAO;
        }

        public DataTable GetById(int stateId)
        {
            SqlParameter param = new SqlParameter("@StateId", SqlDbType.Int);
            param.Value = stateId;

            m_StateDAO.Load("GetStateById", param);
            return m_StateDAO;
        }

        public DataTable GetByName(String stateName)
        {
            SqlParameter param = new SqlParameter("@StateName", SqlDbType.NVarChar, 50);
            param.Value = stateName;

            m_StateDAO.Load("GetStateByName", param);
            return m_StateDAO;
        }

        public int Add(StateInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@StateId", SqlDbType.Int);
            param[0].Value = info.State;

            param[1] = new SqlParameter("@StateName", SqlDbType.NVarChar, 50);
            param[1].Value = info.StateName;

            return m_StateDAO.ExecuteUpdate("AddState", param);
        }

        public int Update(StateInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@StateId", SqlDbType.Int);
            param[0].Value = info.State;

            param[1] = new SqlParameter("@StateName", SqlDbType.NVarChar, 50);
            param[1].Value = info.StateName;

            return m_StateDAO.ExecuteUpdate("UpdateState", param);
        }

        public int Delete(int stateId)
        {
            SqlParameter param = new SqlParameter("@StateId", SqlDbType.Int);
            param.Value = stateId;

            return m_StateDAO.ExecuteUpdate("DeleteState", param);
        }

        public DataRow AddNewRow()
        {
            return m_StateDAO.NewRow();
        }

        public void AddNewState(DataRow m_Row)
        {
            m_StateDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_StateDAO.ExecuteNoneQuery() > 0;
        }
    }
}
